<?php


namespace api\home\controller;


use api\home\model\AddressModel;
use cmf\controller\RestBaseController;
use think\Request;

class AddressController extends RestBaseController
{

    //显示
    public function getAddressList(Request $request)
    {

        $data = AddressModel::field('name,mobile,address,address_more')
            ->where('id','=',$request->userId)
            ->select();

        $this->success('收货列表',$data);
    }

    //添加
    public function createAddress(Request $request)
    {

        $data = input('post.');
        $data['user_id'] = $request->userId;

        $add = AddressModel::create($data);

        $this->success('添加成功',$add);

    }

    //编辑
    public function updateAddress(Request $request)
    {
        $data = input('post.');
        $data['user_id'] = $request->userId;

        //验证查询信息
        $edit = AddressModel::find($request->userId);
        if(!$edit){
            $this->error('未查询到信息');
        }

        $update = AddressModel::where('user_id','=',$request->userId)
            ->update($data);
        if($update){
            $this->success('修改成功',$data);
        }else{
            $this->error('修改失败');
        }

    }

    //删除
    public function delete(Request $request)
    {
        $data = input('post.');
        $data['user_id'] = $request->userId;

        if(AddressModel::destroy($request->userId)){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }

    }


}